package main

import (
	"bytes"
	"io/ioutil"
	"log"
	"net/http"

	"gitee.com/mdgsf/HTTPUseProtoBuf"
	"github.com/golang/protobuf/proto"
)

func main() {

	in := &HTTPUseProtoBuf.MsgInner{}
	in.Name = proto.String("I'm inner.")

	out := &HTTPUseProtoBuf.MsgOuter{}
	out.In = in

	reg := &HTTPUseProtoBuf.Register{}
	reg.Username = proto.String("huangjian")
	reg.Password = proto.String("123")
	reg.Age = proto.Int(28)
	reg.Email = proto.String("1342042894@qq.com")

	money := []int32{100, 200, 300, 400, 500}

	text := []string{"HarryPotter", "Oliver", "Head First design pattern"}

	stu1 := &HTTPUseProtoBuf.Student{}
	stu1.Id = proto.Int32(1)
	stu1.Username = proto.String("huangjian")

	stu2 := &HTTPUseProtoBuf.Student{}
	stu2.Id = proto.Int32(1)
	stu2.Username = proto.String("all")

	stu := []*HTTPUseProtoBuf.Student{}
	stu = append(stu, stu1)
	stu = append(stu, stu2)

	cpx := &HTTPUseProtoBuf.MsgCmplx{}
	cpx.Out = out
	cpx.Reg = reg
	cpx.Money = money
	cpx.Text = text
	cpx.Stu = stu
	cpx.Tag = proto.String("This is a tag.")

	data, err := proto.Marshal(cpx)
	if err != nil {
		log.Fatal("marshaling error:", err)
	}
	body := bytes.NewBuffer(data)

	url := "http://localhost:9090/protoPostCmplx"
	contentType := "application/json;charset=utf-8"
	res, err := http.Post(url, contentType, body)
	if err != nil {
		log.Panic("http post failed")
	}

	result, err := ioutil.ReadAll(res.Body)
	res.Body.Close()

	log.Println(result)
	log.Println(string(result))
}
